//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System.Collections.Generic;

using Mindscape.BackgroundMotion.Core;
using Mindscape.BackgroundMotion.Core.Utilities;
using Mindscape.BackgroundMotion.Model;
using Mindscape.BackgroundMotion.Services.Contracts;
using Mindscape.BackgroundMotion.Services.Contracts.Entities;
using Mindscape.BackgroundMotion.Website.Controllers;
using Mindscape.BackgroundMotion.Website.Infrastructure;

namespace Mindscape.BackgroundMotion.Services
{
  /// <summary>
  /// Implements the IContentService service contract
  /// </summary>
  public class ContentService : IContentService
  {
    /// <summary>
    /// Returns a Content item which is held by the system
    /// </summary>
    /// <param name="id">The unique identifier of the <see ref="Content"/> item</param>
    /// <remarks>The ServiceModuleBehavior attribute initializes the ServiceLocator for calls to this method</remarks>
    [ServiceModuleBehavior]
    public Content GetContent(int id)
    {
      IContributionController controller = ServiceLocator.Get<IContributionController>();
      Contribution contribution = controller.GetContribution(id);
      Content item = DataMapper<Content>.Map(contribution);
      item.AddedBy = contribution.Contributor.Username;
      return item;
    }

    /// <summary>
    /// Fetches <see ref="Content"/> which are currently awaiting moderation
    /// </summary>
    /// <remarks>The ServiceModuleBehavior attribute initializes the ServiceLocator for calls to this method</remarks>
    [ServiceModuleBehavior]
    public Content[] GetContentForModeration()
    {
      IContributionController controller = ServiceLocator.Get<IContributionController>();
      IList<Contribution> unmoderatedContent = controller.GetUnmoderatedContributions();

      Content[] collection = new Content[unmoderatedContent.Count];
      int i = 0;

      foreach (Contribution item in unmoderatedContent)
      {
        collection[i] = DataMapper<Content>.Map(item);
        collection[i].AddedBy = item.Contributor.Username;
        i++;
      }

      return collection;
    }

    /// <summary>
    /// Approves a Content item which is awaiting moderation
    /// </summary>
    /// <param name="id">The unique identifier of the <see ref="Content"/> item to moderate</param>
    /// <remarks>The ServiceModuleBehavior attribute initializes the ServiceLocator for calls to this method</remarks>
    [ServiceModuleBehavior]
    public void ApproveContent(int id)
    {
      IContributionController controller = ServiceLocator.Get<IContributionController>();
      controller.ApproveContribution(id);
    }
  }
}